#include <linux/init.h>
#include <linux/compiler.h>
#include <linux/kconfig.h>
#include <riscv/sbi.h>
#include <riscv/smp.h>
#include <linux/of/early.h>

unsigned long boot_cpu_hartid;
void *_dtb_early_va;
uintptr_t _dtb_early_pa;

__weak void machine_init(void)
{
}

__weak uintptr_t early_pgtable_setup(uintptr_t dtb_pa, uintptr_t start_pa)
{
    _dtb_early_pa = dtb_pa;
    _dtb_early_va = (void *)dtb_pa;

    return 0;
}

__weak void paging_init(void)
{
}

static inline void parse_dtb(void)
{
    early_init_dt_scan(_dtb_early_va, _dtb_early_pa);
}

static inline void unflatten_dtb(void)
{
    unflatten_device_tree();
}

void setup_arch(char **cmdline_p)
{
    parse_dtb();

    sbi_init();

    machine_init();

    paging_init();

    unflatten_dtb();

#ifdef CONFIG_SMP
    setup_smp();
#endif
}

uintptr_t early_setup(int hartid, uintptr_t dtb_pa, uintptr_t start_pa)
{
    uintptr_t satp;

    satp = early_pgtable_setup(dtb_pa, start_pa);

    return satp;
}
